package com.example.bysj.Dao;
//201902104061徐中堂
import com.example.bysj.domain.Degree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
//声明数据库访问类
@Repository("DegreeDao")
public class DegreeDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Degree getById(int id){
        String sql = "select * from Degree where id=?";
        List<Degree> degrees = jdbcTemplate.query(sql, new Object[]{id},new RowMapper<Degree>() {
            @Override
            public Degree mapRow(ResultSet rs, int rowNum) throws SQLException {
                Degree degree = new Degree();
                degree.setId(rs.getInt("id"));
                degree.setNo(rs.getString("no"));
                degree.setDescription(rs.getString("description"));
                degree.setRemarks(rs.getString("remarks"));
                return degree;
            }
        });
        if (!degrees.isEmpty()){
            return degrees.get(0);
        } else
            return null;
    }

    public List<Degree> getAll(){
        String sql = "select * from Degree";
        List<Degree> degrees = jdbcTemplate.query(sql, new RowMapper<Degree>() {
            @Override
            public Degree mapRow(ResultSet rs, int rowNum) throws SQLException {
                Degree degree = new Degree();
                degree.setId(rs.getInt("id"));
                degree.setNo(rs.getString("no"));
                degree.setDescription(rs.getString("description"));
                degree.setRemarks(rs.getString("remarks"));
                return degree;
            }
        });
        return degrees;
    }

    public boolean add(List<Degree> degrees){
        String sql = "INSERT INTO Degree(no,description.remarks) VALUES(?,?,?)";
        for (int i = 0; i<degrees.size(); i++){
            int rowAffected = jdbcTemplate.update(sql,
                    degrees.get(i).getNo(),
                    degrees.get(i).getDescription(),
                    degrees.get(i).getRemarks());
            if (rowAffected != 1){
                return false;
            }
        }
        return true;
    }

    public boolean delete(int id){
        String sql = "delete from Degree where id=?";
        int rowAffected = jdbcTemplate.update(sql,id);
        if (rowAffected!=1){
            return false;
        } else
            return true;
    }

    public boolean update(int id,Degree degree){
        String sql = "update Degree set no=?,description=?,remarks=? where id=?";
        int rowAffected = jdbcTemplate.update(sql,
                degree.getNo(),
                degree.getDescription(),
                degree.getRemarks(),
                id);
        if (rowAffected!=1){
            return false;
        } else {
            return true;
        }
    }
}
